import {timeFormat} from '../../model/utils/common';

export default {
    namespace: 'car',
    state: {
        list : '',
        deleteModal : false,
        addModal : false
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        /**
         * 获取当前车辆
         * 如果没有车辆则默认选择第一辆
         */
        *getCarInfo({ payload }, { put, call, select }){
            // 查询当前车辆列表
            const carList = yield put.resolve({
                type : '$request/get',
                payload : {
                    url : '/api/getCarList'
                }
            });
            if(!carList || !carList.length){
                // wx.removeStorageSync('carNumber');
                // wx.removeStorageSync('oilType');
                return { carList : []};
            }
            // 如果本地存储的有车辆
            const localCar = wx.getStorageSync('carNumber');
            // 判断本地存储的车辆是否存在
            let hasItem = carList.find(item => item.carName == localCar);
            if(!hasItem){
                hasItem = carList[0];
            }
            // 更新本地油类型
            wx.setStorageSync('carNumber', hasItem.carNumber);
            wx.setStorageSync('oilType', hasItem.oilType);
            return {
                carList,
                carInfo: hasItem, // 当前车辆信息
                carNumber: hasItem.carNumber
            };

        },

        *init({ payload }, { put, call, select }) {
            const list = yield put.resolve({
                type : '$request/get',
                payload : {
                    url : '/api/getCarList'
                }
            });
            const hasCar = list && list.length;
            yield put({
                type : 'save',
                payload : {
                    list,
                    addModal: !hasCar
                }
            });
            
        },
        *addCar({ payload }, { put, call, select }){
            const url = payload.id ? '/api/editCar': '/api/addCar';
            const res = yield put.resolve({
                type : '$request/post',
                payload : {
                    url ,
                    data : {
                        id: payload.id,
                        number : payload.number,
                        oilType: payload.oilType
                    }
                }
            });
            yield put({
                type : 'save',
                payload : {
                    addModal : false
                }
            });
            yield put.resolve({
                type : '$msg/showToast',
                payload : {
                    title : '保存成功',
                }
            });
            if(payload.callback){
                return payload.callback(payload.number);
            }
            yield put({
                type : 'init'
            });
        },
        *deleteCar({ payload }, { put, call, select }){
            yield put.resolve({
                type : '$request/post',
                payload : {
                    url : '/api/deleteCar',
                    data : {
                        id : payload.id
                    }
                }
            });
            yield put({
                type : 'save',
                payload : {
                    deleteModal : false
                }
            });
            yield put.resolve({
                type : '$msg/showToast',
                payload : {
                    title : '删除成功',
                }
            });
            yield put({
                type : 'init'
            });
        },
        *valideInput({ payload }, { put, call, select }){
            const result = yield put.resolve({
                type : '$request/post',
                payload : {
                    url : '/api/msgSecCheck',
                    data : {
                        content : payload.value
                    }
                }
            });
        }
    }
};